<template lang="">
    <div>
<el-dialog :before-close="cancel" center :title="isAdd?'添加商品分类':'修改商品分类'" :visible.sync="isShow">
  <el-form :model="cateForm" :rules="rules" ref="userinfo" label-width="100px" class="demo-ruleForm">
          <el-form-item label="上级分类" prop="pid" :label-width="formLabelWidth">
      <el-select v-model="cateForm.pid" placeholder="请选择">
        <el-option label="顶级分类" :value="0"></el-option>
        <el-option
        v-for="item in catelist"
        :key="item.id"
        :value="item.id"
        :label="item.catename"
        ></el-option>
      </el-select>
    </el-form-item>
          <el-form-item label="商品分类名称" prop="catename" :label-width="formLabelWidth">
    <el-input v-model="cateForm.catename"></el-input>
     </el-form-item>
     <el-form-item
          label="图片"
          v-if="cateForm.pid != 0"
          :label-width="formLabelWidth"
        >
          <!-- 图片上传组件
          action	必选参数，上传的地址
          list-type	文件列表的类型	string	text/picture/picture-card
          on-preview	点击文件列表中已上传的文件时的钩子	function(file)
          on-remove	文件列表移除文件时的钩子	function(file, fileList)
          on-change	文件状态改变时的钩子，添加文件、上传成功和上传失败时都会被调用	function(file, fileList)
          file-list	上传的文件列表, 例如: [{name: 'food.jpg', url: 'https://xxx.cdn.com/xxx.jpg'}]	array
          on-exceed	文件超出个数限制时的钩子	function(files, fileList)
          -->
          <el-upload
            action="#"
            list-type="picture-card"
            :on-preview="onPreview"
            :on-remove="onRemove"
            :on-change="changeFile"
            :http-request="uploadHttpRequest"
            :file-list="fileList"
            :limit="1"
            :on-exceed="handleExceed"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
          <!-- 图片预览组件 -->
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="" />
          </el-dialog>
        </el-form-item>
       <el-form-item  label="状态">
   <el-switch
    v-model="cateForm.status"
    active-color="#13ce66"
    inactive-color="#ff4949"
    :active-value="1"
    :inactive-value="2">
  </el-switch>
</el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="cancel">取 消</el-button>
    <el-button type="primary" @click="comfirm">确 定</el-button>
  </div>
</el-dialog>
    </div>
</template>
<script>
import { mapGetters, mapActions } from 'vuex';
import { cateAdd, cateInfo, cateEdit } from '../../request/api'
export default {
  props: ['isShow', 'isAdd'],
  data() {
    return {
      rules: {
        catename: [
          { required: true, message: '请输入商品分类名称', trigger: 'blur' },
          { min: 2, max: 6, message: '长度在 2 到 6 个字符', trigger: 'blur' }
        ],
      },
      pid: [
        { required: true, message: "请选择上级商品分类", trigger: "change" },
      ],
      cateForm: {
        status: 1,
        pid: 0,//上级分类编号
        catename: '',//商品分类名称
        img: '',//(文件，一般用于二级分类)
      },
      dialogVisible: false, //是否渲染图片弹框
      dialogImageUrl: "", //图片地址
      fileList: [],
      formLabelWidth: "120px",
    }
  },
  computed: {
    ...mapGetters({
      catelist: 'cate/getCateList'
    })
  },
  methods: {
    ...mapActions({
      getCate: "cate/changeCateAction"
    }),
    cancel() {
      this.$emit('cancel', false),
        this.reset()
    },
    uploadHttpRequest(data){},
    handleExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length
        } 个文件`
      );
    },
    //文件修改时触发
    changeFile(file, filelist) {
      console.log(file, filelist, "change");
      this.cateForm.img = file.raw;
    },
    //图片预览
    onPreview(file) {
      console.log(file, "图片预览");
      //给预览图片赋值
      this.dialogImageUrl = file.url;
      //打开弹框
      this.dialogVisible = true;
    },
    onRemove(file, filelist) {
      console.log(file, filelist, "移除");
    },
    // 表单清空
    reset() {
      this.cateForm = {
        status: 1,
        pid: '',//上级分类编号
        catename: '',//商品分类名称
        img: '',//(文件，一般用于二级分类)
      };
      // 表单清空验证
      this.$refs.userinfo.resetFields();
      // 清空文件列表
      this.fileList = []
    },
    lockup(id) {
      // 调用一条数据查询
      cateInfo({ id, })
        .then(res => {
          if (res.code == 200) {
            this.cateForm = res.list;
            this.cateForm.id = id;
            this.fileList = [{ url: this.$img + res.list.img }];
          }
        })
    },
    // 封装一个确定事件
    comfirm() {
      this.$refs.userinfo.validate((val) => {
        if (val) {
          //判断是否为添加还是编辑
          if (this.isAdd) {
            console.log(this.cateForm, '表单对象');
            // 添加接口
            cateAdd(this.cateForm).then((res) => {
              // console.log(res, "响应");
              if (res.code == 200) {
                //添加成功
                this.$message.success(res.msg);
                this.cancel();
                this.getCate();
              } else {
                this.$message.error(res.msg);
              }
            });
          } else {
            // console.log('编辑接口');
            cateEdit(this.cateForm).then(res => {
              if (res.code == 200) {
                this.$message.success(res.msg)
                this.cancel();
                this.getCate();
              } else {
                this.$message.error(res.msg);
              }
            })
          }
        }
      })
    },
  },
}
</script>
<style lang="less" scoped>
</style>